home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / apt / cache.py < prev    next >
Text File  |  2009-09-25  |  18KB  |  568 lines

  1. # cache.py - apt cache abstraction
  2. #
  3. #  Copyright (c) 2005-2009 Canonical
  4. #
  5. #  Author: Michael Vogt <michael.vogt@ubuntu.com>
  6. #
  7. #  This program is free software; you can redistribute it and/or
  8. #  modify it under the terms of the GNU General Public License as
  9. #  published by the Free Software Foundation; either version 2 of the
  10. #  License, or (at your option) any later version.
  11. #
  12. #  This program is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16. #
  17. #  You should have received a copy of the GNU General Public License
  18. #  along with this program; if not, write to the Free Software
  19. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. #  USA
  21.  
  22. import os
  23. import weakref
  24.  
  25. import apt_pkg
  26. from apt import Package
  27. import apt.progress
  28.  
  29.  
  30. class FetchCancelledException(IOError):
  31.     """Exception that is thrown when the user cancels a fetch operation."""
  32.  
  33.  
  34. class FetchFailedException(IOError):
  35.     """Exception that is thrown when fetching fails."""
  36.  
  37.  
  38. class LockFailedException(IOError):
  39.     """Exception that is thrown when locking fails."""
  40.  
  41.  
  42. class Cache(object):
  43.     """Dictionary-like package cache.
  44.  
  45.     This class has all the packages that are available in it's
  46.     dictionary
  47.     """
  48.  
  49.     def __init__(self, progress=None, rootdir=None, memonly=False):
  50.         self._callbacks = {}
  51.         self._weakref = weakref.WeakValueDictionary()
  52.         self._set = set()
  53.         if memonly:
  54.             # force apt to build its caches in memory
  55.             apt_pkg.Config.Set("Dir::Cache::pkgcache", "")
  56.         if rootdir:
  57.             if os.path.exists(rootdir+"/etc/apt/apt.conf"):
  58.                 apt_pkg.ReadConfigFile(apt_pkg.Config,
  59.                                        rootdir + "/etc/apt/apt.conf")
  60.             if os.path.isdir(rootdir+"/etc/apt/apt.conf.d"):
  61.                 apt_pkg.ReadConfigDir(apt_pkg.Config,
  62.                                       rootdir + "/etc/apt/apt.conf.d")
  63.             apt_pkg.Config.Set("Dir", rootdir)
  64.             apt_pkg.Config.Set("Dir::State::status",
  65.                                rootdir + "/var/lib/dpkg/status")
  66.             # create required dirs/files when run with special rootdir
  67.             # automatically
  68.             self._check_and_create_required_dirs(rootdir)
  69.             # Call InitSystem so the change to Dir::State::Status is actually
  70.             # recognized (LP: #320665)
  71.             apt_pkg.InitSystem()
  72.         self.open(progress)
  73.  
  74.     def _check_and_create_required_dirs(self, rootdir):
  75.         """
  76.         check if the required apt directories/files are there and if
  77.         not create them
  78.         """
  79.         files = ["/var/lib/dpkg/status",
  80.                  "/etc/apt/sources.list",
  81.                 ]
  82.         dirs = ["/var/lib/dpkg",
  83.                 "/etc/apt/",
  84.                 "/var/cache/apt/archives/partial",
  85.                 "/var/lib/apt/lists/partial",
  86.                ]
  87.         for d in dirs:
  88.             if not os.path.exists(rootdir+d):
  89.                 print "creating: ",rootdir+d
  90.                 os.makedirs(rootdir+d)
  91.         for f in files:
  92.             if not os.path.exists(rootdir+f):
  93.                 open(rootdir+f,"w")
  94.  
  95.     def _runCallbacks(self, name):
  96.         """ internal helper to run a callback """
  97.         if name in self._callbacks:
  98.             for callback in self._callbacks[name]:
  99.                 callback()
  100.  
  101.     def open(self, progress=None):
  102.         """ Open the package cache, after that it can be used like
  103.             a dictionary
  104.         """
  105.         if progress is None:
  106.             progress = apt.progress.OpProgress()
  107.         self._runCallbacks("cache_pre_open")
  108.         self._cache = apt_pkg.GetCache(progress)
  109.         self._depcache = apt_pkg.GetDepCache(self._cache)
  110.         self._records = apt_pkg.GetPkgRecords(self._cache)
  111.         self._list = apt_pkg.GetPkgSourceList()
  112.         self._list.ReadMainList()
  113.         self._set.clear()
  114.         self._weakref.clear()
  115.  
  116.         progress.Op = "Building data structures"
  117.         i=last=0
  118.         size=len(self._cache.Packages)
  119.         for pkg in self._cache.Packages:
  120.             if progress is not None and last+100 < i:
  121.                 progress.update(i/float(size)*100)
  122.                 last=i
  123.             # drop stuff with no versions (cruft)
  124.             if len(pkg.VersionList) > 0:
  125.                 self._set.add(pkg.Name)
  126.  
  127.             i += 1
  128.  
  129.         progress.done()
  130.         self._runCallbacks("cache_post_open")
  131.  
  132.     def __getitem__(self, key):
  133.         """ look like a dictionary (get key) """
  134.         try:
  135.             return self._weakref[key]
  136.         except KeyError:
  137.             if key in self._set:
  138.                 key = str(key)
  139.                 pkg = self._weakref[key] = Package(self, self._cache[key])
  140.                 return pkg
  141.             else:
  142.                 raise KeyError('The cache has no package named %r' % key)
  143.  
  144.     def __iter__(self):
  145.         for pkgname in self._set:
  146.             yield self[pkgname]
  147.         raise StopIteration
  148.  
  149.     def has_key(self, key):
  150.         return (key in self._set)
  151.  
  152.     def __contains__(self, key):
  153.         return (key in self._set)
  154.  
  155.     def __len__(self):
  156.         return len(self._set)
  157.  
  158.     def keys(self):
  159.         return list(self._set)
  160.  
  161.     def getChanges(self):
  162.         """ Get the marked changes """
  163.         changes = []
  164.         for p in self:
  165.             if p.markedUpgrade or p.markedInstall or p.markedDelete or \
  166.                p.markedDowngrade or p.markedReinstall:
  167.                 changes.append(p)
  168.         return changes
  169.  
  170.     def upgrade(self, distUpgrade=False):
  171.         """ Upgrade the all package, DistUpgrade will also install
  172.             new dependencies
  173.         """
  174.         self.cachePreChange()
  175.         self._depcache.Upgrade(distUpgrade)
  176.         self.cachePostChange()
  177.  
  178.     @property
  179.     def requiredDownload(self):
  180.         """Get the size of the packages that are required to download."""
  181.         pm = apt_pkg.GetPackageManager(self._depcache)
  182.         fetcher = apt_pkg.GetAcquire()
  183.         pm.GetArchives(fetcher, self._list, self._records)
  184.         return fetcher.FetchNeeded
  185.  
  186.     @property
  187.     def additionalRequiredSpace(self):
  188.         """Get the size of the additional required space on the fs."""
  189.         return self._depcache.UsrSize
  190.  
  191.     @property
  192.     def reqReinstallPkgs(self):
  193.         """Return the packages not downloadable packages in reqreinst state."""
  194.         reqreinst = set()
  195.         for pkg in self:
  196.             if (not pkg.candidate.downloadable and
  197.                 (pkg._pkg.InstState == apt_pkg.InstStateReInstReq or
  198.                  pkg._pkg.InstState == apt_pkg.InstStateHoldReInstReq)):
  199.                 reqreinst.add(pkg.name)
  200.         return reqreinst
  201.  
  202.     def _runFetcher(self, fetcher):
  203.         # do the actual fetching
  204.         res = fetcher.Run()
  205.  
  206.         # now check the result (this is the code from apt-get.cc)
  207.         failed = False
  208.         transient = False
  209.         errMsg = ""
  210.         for item in fetcher.Items:
  211.             if item.Status == item.StatDone:
  212.                 continue
  213.             if item.StatIdle:
  214.                 transient = True
  215.                 continue
  216.             errMsg += "Failed to fetch %s %s\n" % (item.DescURI,
  217.                                                    item.ErrorText)
  218.             failed = True
  219.  
  220.         # we raise a exception if the download failed or it was cancelt
  221.         if res == fetcher.ResultCancelled:
  222.             raise FetchCancelledException(errMsg)
  223.         elif failed:
  224.             raise FetchFailedException(errMsg)
  225.         return res
  226.  
  227.     def _fetchArchives(self, fetcher, pm):
  228.         """ fetch the needed archives """
  229.  
  230.         # get lock
  231.         lockfile = apt_pkg.Config.FindDir("Dir::Cache::Archives") + "lock"
  232.         lock = apt_pkg.GetLock(lockfile)
  233.         if lock < 0:
  234.             raise LockFailedException("Failed to lock %s" % lockfile)
  235.  
  236.         try:
  237.             # this may as well throw a SystemError exception
  238.             if not pm.GetArchives(fetcher, self._list, self._records):
  239.                 return False
  240.             # now run the fetcher, throw exception if something fails to be
  241.             # fetched
  242.             return self._runFetcher(fetcher)
  243.         finally:
  244.             os.close(lock)
  245.  
  246.     def isVirtualPackage(self, pkgname):
  247.         """Return whether the package is a virtual package."""
  248.         pkg = self._cache[pkgname]
  249.         return bool(pkg.ProvidesList and not pkg.VersionList)
  250.  
  251.     def getProvidingPackages(self, virtual):
  252.         """
  253.         Return a list of packages which provide the virtual package of the
  254.         specified name
  255.         """
  256.         providers = []
  257.         try:
  258.             vp = self._cache[virtual]
  259.             if len(vp.VersionList) != 0:
  260.                 return providers
  261.         except KeyError:
  262.             return providers
  263.         for pkg in self:
  264.             v = self._depcache.GetCandidateVer(pkg._pkg)
  265.             if v is None:
  266.                 continue
  267.             for p in v.ProvidesList:
  268.                 if virtual == p[0]:
  269.                     # we found a pkg that provides this virtual pkg
  270.                     providers.append(pkg)
  271.         return providers
  272.  
  273.     def update(self, fetchProgress=None, pulseInterval=0):
  274.         " run the equivalent of apt-get update "
  275.         lockfile = apt_pkg.Config.FindDir("Dir::State::Lists") + "lock"
  276.         lock = apt_pkg.GetLock(lockfile)
  277.         if lock < 0:
  278.             raise LockFailedException("Failed to lock %s" % lockfile)
  279.  
  280.         try:
  281.             if fetchProgress is None:
  282.                 fetchProgress = apt.progress.FetchProgress()
  283.             return self._cache.Update(fetchProgress, self._list, pulseInterval)
  284.         finally:
  285.             os.close(lock)
  286.  
  287.     def installArchives(self, pm, installProgress):
  288.         installProgress.startUpdate()
  289.         res = installProgress.run(pm)
  290.         installProgress.finishUpdate()
  291.         return res
  292.  
  293.     def commit(self, fetchProgress=None, installProgress=None):
  294.         """ Apply the marked changes to the cache """
  295.         # FIXME:
  296.         # use the new acquire/pkgmanager interface here,
  297.         # raise exceptions when a download or install fails
  298.         # and send proper error strings to the application.
  299.         # Current a failed download will just display "error"
  300.         # which is less than optimal!
  301.  
  302.         if fetchProgress is None:
  303.             fetchProgress = apt.progress.FetchProgress()
  304.         if installProgress is None:
  305.             installProgress = apt.progress.InstallProgress()
  306.  
  307.         pm = apt_pkg.GetPackageManager(self._depcache)
  308.         fetcher = apt_pkg.GetAcquire(fetchProgress)
  309.         while True:
  310.             # fetch archives first
  311.             res = self._fetchArchives(fetcher, pm)
  312.  
  313.             # then install
  314.             res = self.installArchives(pm, installProgress)
  315.             if res == pm.ResultCompleted:
  316.                 break
  317.             elif res == pm.ResultFailed:
  318.                 raise SystemError("installArchives() failed")
  319.             elif res == pm.ResultIncomplete:
  320.                  pass
  321.             else:
  322.                  raise SystemError("internal-error: unknown result code from InstallArchives: %s" % res)
  323.             # reload the fetcher for media swaping
  324.             fetcher.Shutdown()
  325.         return (res == pm.ResultCompleted)
  326.  
  327.     def clear(self):
  328.         """ Unmark all changes """
  329.         self._depcache.Init()
  330.  
  331.     # cache changes
  332.  
  333.     def cachePostChange(self):
  334.         " called internally if the cache has changed, emit a signal then "
  335.         self._runCallbacks("cache_post_change")
  336.  
  337.     def cachePreChange(self):
  338.         """ called internally if the cache is about to change, emit
  339.             a signal then """
  340.         self._runCallbacks("cache_pre_change")
  341.  
  342.     def actiongroup(self):
  343.         """Return an ActionGroup() object for the current cache.
  344.  
  345.         Action groups can be used to speedup actions. The action group is
  346.         active as soon as it is created, and disabled when the object is
  347.         deleted or when release() is called.
  348.         """
  349.         return apt_pkg.GetPkgActionGroup(self._depcache)
  350.  
  351.     def connect(self, name, callback):
  352.         """ connect to a signal, currently only used for
  353.             cache_{post,pre}_{changed,open} """
  354.         if not name in self._callbacks:
  355.             self._callbacks[name] = []
  356.         self._callbacks[name].append(callback)
  357.  
  358.     @property
  359.     def broken_count(self):
  360.         """Return the number of packages with broken dependencies."""
  361.         return self._depcache.BrokenCount
  362.  
  363.     @property
  364.     def delete_count(self):
  365.         """Return the number of packages marked for deletion."""
  366.         return self._depcache.DelCount
  367.  
  368.     @property
  369.     def install_count(self):
  370.         """Return the number of packages marked for installation."""
  371.         return self._depcache.InstCount
  372.  
  373.     @property
  374.     def keep_count(self):
  375.         """Return the number of packages marked as keep."""
  376.         return self._depcache.KeepCount
  377.  
  378.  
  379. class ProblemResolver(object):
  380.     """Resolve problems due to dependencies and conflicts.
  381.  
  382.     The first argument 'cache' is an instance of apt.Cache.
  383.     """
  384.  
  385.     def __init__(self, cache):
  386.         self._resolver = apt_pkg.GetPkgProblemResolver(cache._depcache)
  387.  
  388.     def clear(self, package):
  389.         """Reset the package to the default state."""
  390.         self._resolver.Clear(package._pkg)
  391.  
  392.     def install_protect(self):
  393.         """mark protected packages for install or removal."""
  394.         self._resolver.InstallProtect()
  395.  
  396.     def protect(self, package):
  397.         """Protect a package so it won't be removed."""
  398.         self._resolver.Protect(package._pkg)
  399.  
  400.     def remove(self, package):
  401.         """Mark a package for removal."""
  402.         self._resolver.Remove(package._pkg)
  403.  
  404.     def resolve(self):
  405.         """Resolve dependencies, try to remove packages where needed."""
  406.         self._resolver.Resolve()
  407.  
  408.     def resolve_by_keep(self):
  409.         """Resolve dependencies, do not try to remove packages."""
  410.         self._resolver.ResolveByKeep()
  411.  
  412.  
  413. # ----------------------------- experimental interface
  414.  
  415.  
  416. class Filter(object):
  417.     """ Filter base class """
  418.  
  419.     def apply(self, pkg):
  420.         """ Filter function, return True if the package matchs a
  421.             filter criteria and False otherwise
  422.         """
  423.         return True
  424.  
  425.  
  426. class MarkedChangesFilter(Filter):
  427.     """ Filter that returns all marked changes """
  428.  
  429.     def apply(self, pkg):
  430.         if pkg.markedInstall or pkg.markedDelete or pkg.markedUpgrade:
  431.             return True
  432.         else:
  433.             return False
  434.  
  435.  
  436. class FilteredCache(object):
  437.     """ A package cache that is filtered.
  438.  
  439.         Can work on a existing cache or create a new one
  440.     """
  441.  
  442.     def __init__(self, cache=None, progress=None):
  443.         if cache is None:
  444.             self.cache = Cache(progress)
  445.         else:
  446.             self.cache = cache
  447.         self.cache.connect("cache_post_change", self.filterCachePostChange)
  448.         self.cache.connect("cache_post_open", self.filterCachePostChange)
  449.         self._filtered = {}
  450.         self._filters = []
  451.  
  452.     def __len__(self):
  453.         return len(self._filtered)
  454.  
  455.     def __getitem__(self, key):
  456.         return self.cache[key]
  457.  
  458.     def __iter__(self):
  459.         for pkgname in self._filtered:
  460.             yield self.cache[pkgname]
  461.  
  462.     def keys(self):
  463.         return self._filtered.keys()
  464.  
  465.     def has_key(self, key):
  466.         return (key in self._filtered)
  467.  
  468.     def __contains__(self, key):
  469.         return (key in self._filtered)
  470.  
  471.     def _reapplyFilter(self):
  472.         " internal helper to refilter "
  473.         self._filtered = {}
  474.         for pkg in self.cache:
  475.             for f in self._filters:
  476.                 if f.apply(pkg):
  477.                     self._filtered[pkg.name] = 1
  478.                     break
  479.  
  480.     def setFilter(self, filter):
  481.         """Set the current active filter."""
  482.         self._filters = []
  483.         self._filters.append(filter)
  484.         #self._reapplyFilter()
  485.         # force a cache-change event that will result in a refiltering
  486.         self.cache.cachePostChange()
  487.  
  488.     def filterCachePostChange(self):
  489.         """Called internally if the cache changes, emit a signal then."""
  490.         #print "filterCachePostChange()"
  491.         self._reapplyFilter()
  492.  
  493. #    def connect(self, name, callback):
  494. #        self.cache.connect(name, callback)
  495.  
  496.     def __getattr__(self, key):
  497.         """we try to look exactly like a real cache."""
  498.         #print "getattr: %s " % key
  499.         return getattr(self.cache, key)
  500.  
  501.  
  502. def cache_pre_changed():
  503.     print "cache pre changed"
  504.  
  505.  
  506. def cache_post_changed():
  507.     print "cache post changed"
  508.  
  509.  
  510. # internal test code
  511. if __name__ == "__main__":
  512.     print "Cache self test"
  513.     apt_pkg.init()
  514.     c = Cache(apt.progress.OpTextProgress())
  515.     c.connect("cache_pre_change", cache_pre_changed)
  516.     c.connect("cache_post_change", cache_post_changed)
  517.     print ("aptitude" in c)
  518.     p = c["aptitude"]
  519.     print p.name
  520.     print len(c)
  521.  
  522.     for pkg in c.keys():
  523.         x= c[pkg].name
  524.  
  525.     c.upgrade()
  526.     changes = c.getChanges()
  527.     print len(changes)
  528.     for p in changes:
  529.         #print p.name
  530.         x = p.name
  531.  
  532.  
  533.     # see if fetching works
  534.     for d in ["/tmp/pytest", "/tmp/pytest/partial"]:
  535.         if not os.path.exists(d):
  536.             os.mkdir(d)
  537.     apt_pkg.Config.Set("Dir::Cache::Archives", "/tmp/pytest")
  538.     pm = apt_pkg.GetPackageManager(c._depcache)
  539.     fetcher = apt_pkg.GetAcquire(apt.progress.TextFetchProgress())
  540.     c._fetchArchives(fetcher, pm)
  541.     #sys.exit(1)
  542.  
  543.     print "Testing filtered cache (argument is old cache)"
  544.     f = FilteredCache(c)
  545.     f.cache.connect("cache_pre_change", cache_pre_changed)
  546.     f.cache.connect("cache_post_change", cache_post_changed)
  547.     f.cache.upgrade()
  548.     f.setFilter(MarkedChangesFilter())
  549.     print len(f)
  550.     for pkg in f.keys():
  551.         #print c[pkg].name
  552.         x = f[pkg].name
  553.  
  554.     print len(f)
  555.  
  556.     print "Testing filtered cache (no argument)"
  557.     f = FilteredCache(progress=apt.progress.OpTextProgress())
  558.     f.cache.connect("cache_pre_change", cache_pre_changed)
  559.     f.cache.connect("cache_post_change", cache_post_changed)
  560.     f.cache.upgrade()
  561.     f.setFilter(MarkedChangesFilter())
  562.     print len(f)
  563.     for pkg in f.keys():
  564.         #print c[pkg].name
  565.         x = f[pkg].name
  566.  
  567.     print len(f)
  568.